home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cinidemo / cinifile.cls next >
Text File  |  1998-05-10  |  9KB  |  273 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "cInifile"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. ' =========================================================
  12. ' Class:    cIniFile
  13. ' Author:   Steve McMahon
  14. ' Date  :   21 Feb 1997
  15. '
  16. ' A nice class wrapper around the INIFile functions
  17. ' Allows searching,deletion,modification and addition
  18. ' of Keys or Values.
  19. '
  20. ' Updated 10 May 1998 for VB5.
  21. '   * Added EnumerateAllSections method
  22. '   * Added Load and Save form position methods
  23. ' =========================================================
  24.  
  25. Private m_sPath As String
  26. Private m_sKey As String
  27. Private m_sSection As String
  28. Private m_sDefault As String
  29. Private m_lLastReturnCode As Long
  30.  
  31. #If Win32 Then
  32.     ' Profile String functions:
  33.     Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
  34.     Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  35. #Else
  36.     ' Profile String functions:
  37.     Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Integer
  38.     Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  39. #End If
  40.  
  41. Property Get LastReturnCode() As Long
  42.     LastReturnCode = m_lLastReturnCode
  43. End Property
  44. Property Get Success() As Boolean
  45.     Success = (m_lLastReturnCode <> 0)
  46. End Property
  47. Property Let Default(sDefault As String)
  48.     m_sDefault = sDefault
  49. End Property
  50. Property Get Default() As String
  51.     Default = m_sDefault
  52. End Property
  53. Property Let Path(sPath As String)
  54.     m_sPath = sPath
  55. End Property
  56. Property Get Path() As String
  57.     Path = m_sPath
  58. End Property
  59. Property Let Key(sKey As String)
  60.     m_sKey = sKey
  61. End Property
  62. Property Get Key() As String
  63.     Key = m_sKey
  64. End Property
  65. Property Let Section(sSection As String)
  66.     m_sSection = sSection
  67. End Property
  68. Property Get Section() As String
  69.     Section = m_sSection
  70. End Property
  71. Property Get Value() As String
  72. Dim sBuf As String
  73. Dim iSize As String
  74. Dim iRetCode As Integer
  75.  
  76.     sBuf = Space$(255)
  77.     iSize = Len(sBuf)
  78.     iRetCode = GetPrivateProfileString(m_sSection, m_sKey, m_sDefault, sBuf, iSize, m_sPath)
  79.     If (iSize > 0) Then
  80.         Value = Left$(sBuf, iRetCode)
  81.     Else
  82.         Value = ""
  83.     End If
  84.  
  85. End Property
  86. Property Let Value(sValue As String)
  87. Dim iPos As Integer
  88.     ' Strip chr$(0):
  89.     iPos = InStr(sValue, Chr$(0))
  90.     Do While iPos <> 0
  91.         sValue = Left$(sValue, (iPos - 1)) & Mid$(sValue, (iPos + 1))
  92.         iPos = InStr(sValue, Chr$(0))
  93.     Loop
  94.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, m_sKey, sValue, m_sPath)
  95. End Property
  96. Public Sub DeleteKey()
  97.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, m_sKey, 0&, m_sPath)
  98. End Sub
  99. Public Sub DeleteSection()
  100.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, 0&, 0&, m_sPath)
  101. End Sub
  102. Property Get INISection() As String
  103. Dim sBuf As String
  104. Dim iSize As String
  105. Dim iRetCode As Integer
  106.  
  107.     sBuf = Space$(8192)
  108.     iSize = Len(sBuf)
  109.     iRetCode = GetPrivateProfileString(m_sSection, 0&, m_sDefault, sBuf, iSize, m_sPath)
  110.     If (iSize > 0) Then
  111.         INISection = Left$(sBuf, iRetCode)
  112.     Else
  113.         INISection = ""
  114.     End If
  115.  
  116. End Property
  117. Property Let INISection(sSection As String)
  118.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, 0&, sSection, m_sPath)
  119. End Property
  120. Property Get Sections() As String
  121. Dim sBuf As String
  122. Dim iSize As String
  123. Dim iRetCode As Integer
  124.  
  125.     sBuf = Space$(8192)
  126.     iSize = Len(sBuf)
  127.     iRetCode = GetPrivateProfileString(0&, 0&, m_sDefault, sBuf, iSize, m_sPath)
  128.     If (iSize > 0) Then
  129.         Sections = Left$(sBuf, iRetCode)
  130.     Else
  131.         Sections = ""
  132.     End If
  133.  
  134. End Property
  135. Public Sub EnumerateCurrentSection(ByRef sKey() As String, ByRef iCount As Long)
  136. Dim sSection As String
  137. Dim iPos As Long
  138. Dim iNextPos As Long
  139. Dim sCur As String
  140.     
  141.     iCount = 0
  142.     Erase sKey
  143.     sSection = INISection
  144.     If (Len(sSection) > 0) Then
  145.         iPos = 1
  146.         iNextPos = InStr(iPos, sSection, Chr$(0))
  147.         Do While iNextPos <> 0
  148.             sCur = Mid$(sSection, iPos, (iNextPos - iPos))
  149.             If (sCur <> Chr$(0)) Then
  150.                 iCount = iCount + 1
  151.                 ReDim Preserve sKey(1 To iCount) As String
  152.                 sKey(iCount) = Mid$(sSection, iPos, (iNextPos - iPos))
  153.                 iPos = iNextPos + 1
  154.                 iNextPos = InStr(iPos, sSection, Chr$(0))
  155.             End If
  156.         Loop
  157.     End If
  158. End Sub
  159. Public Sub EnumerateAllSections(ByRef sSections() As String, ByRef iCount As Long)
  160. Dim sIniFile As String
  161. Dim iPos As Long
  162. Dim iNextPos As Long
  163. Dim sCur As String
  164.     
  165.     iCount = 0
  166.     Erase sSections
  167.     sIniFile = Sections
  168.     If (Len(sIniFile) > 0) Then
  169.         iPos = 1
  170.         iNextPos = InStr(iPos, sIniFile, Chr$(0))
  171.         Do While iNextPos <> 0
  172.             If (iNextPos <> iPos) Then
  173.                 sCur = Mid$(sIniFile, iPos, (iNextPos - iPos))
  174.                 iCount = iCount + 1
  175.                 ReDim Preserve sSections(1 To iCount) As String
  176.                 sSections(iCount) = sCur
  177.             End If
  178.             iPos = iNextPos + 1
  179.             iNextPos = InStr(iPos, sIniFile, Chr$(0))
  180.         Loop
  181.     End If
  182.  
  183. End Sub
  184. Public Sub SaveFormPosition(ByRef frmThis As Object)
  185. Dim sSaveKey As String
  186. Dim sSaveDefault As String
  187. On Error GoTo SaveError
  188.     sSaveKey = Key
  189.     If Not (frmThis.WindowState = vbMinimized) Then
  190.         Key = "Maximised"
  191.         Value = (frmThis.WindowState = vbMaximized) * -1
  192.         If (frmThis.WindowState <> vbMaximized) Then
  193.             Key = "Left"
  194.             Value = frmThis.Left
  195.             Key = "Top"
  196.             Value = frmThis.Top
  197.             Key = "Width"
  198.             Value = frmThis.Width
  199.             Key = "Height"
  200.             Value = frmThis.Height
  201.         End If
  202.     End If
  203.     Key = sSaveKey
  204.     Exit Sub
  205. SaveError:
  206.     Key = sSaveKey
  207.     m_lLastReturnCode = 0
  208.     Exit Sub
  209. End Sub
  210. Public Sub LoadFormPosition(ByRef frmThis As Object, Optional ByRef lMinWidth = 3000, Optional ByRef lMinHeight = 3000)
  211. Dim sSaveKey As String
  212. Dim sSaveDefault As String
  213. Dim lLeft As Long
  214. Dim lTOp As Long
  215. Dim lWidth As Long
  216. Dim lHeight As Long
  217. On Error GoTo LoadError
  218.     sSaveKey = Key
  219.     sSaveDefault = Default
  220.     Default = "FAIL"
  221.     Key = "Left"
  222.     lLeft = CLngDefault(Value, frmThis.Left)
  223.     Key = "Top"
  224.     lTOp = CLngDefault(Value, frmThis.Top)
  225.     Key = "Width"
  226.     lWidth = CLngDefault(Value, frmThis.Width)
  227.     If (lWidth < lMinWidth) Then lWidth = lMinWidth
  228.     Key = "Height"
  229.     lHeight = CLngDefault(Value, frmThis.Height)
  230.     If (lHeight < lMinHeight) Then lHeight = lMinHeight
  231.     If (lLeft < 4 * Screen.TwipsPerPixelX) Then lLeft = 4 * Screen.TwipsPerPixelX
  232.     If (lTOp < 4 * Screen.TwipsPerPixelY) Then lTOp = 4 * Screen.TwipsPerPixelY
  233.     If (lLeft + lWidth > Screen.Width - 4 * Screen.TwipsPerPixelX) Then
  234.         lLeft = Screen.Width - 4 * Screen.TwipsPerPixelX - lWidth
  235.         If (lLeft < 4 * Screen.TwipsPerPixelX) Then lLeft = 4 * Screen.TwipsPerPixelX
  236.         If (lLeft + lWidth > Screen.Width - 4 * Screen.TwipsPerPixelX) Then
  237.             lWidth = Screen.Width - lLeft - 4 * Screen.TwipsPerPixelX
  238.         End If
  239.     End If
  240.     If (lTOp + lHeight > Screen.Height - 4 * Screen.TwipsPerPixelY) Then
  241.         lTOp = Screen.Height - 4 * Screen.TwipsPerPixelY - lHeight
  242.